package com.selfdriving.map;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiInfo;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.mapapi.utils.DistanceUtil;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.bean.Location;
import com.selfdriving.bean.ScenicSpots;
import com.selfdriving.utils.LogUtil;

public class InitMap
{
	// 定位相关
	private MyLocation myloaction;
	// 搜索相关
	private MKSearch search = null;
	private Handler handler;
	private Context context;
	private GeoPoint mpoint = null;
	public BaseApplication app;
	String keyWord;
	LocationClient mLocClient;

	/**
	 * 定位，搜索景点的构造
	 * 
	 * @param context
	 * @param handler
	 * @param requestLocButton
	 * @param list
	 */
	public InitMap(Context context, Handler handler, String keyWord)
	{
		this.context = context;
		this.handler = handler;
		this.keyWord = keyWord;
		myloaction = new MyLocation();
		map();
	}

	/**
	 * 初始化地图控件
	 */
	public void map()
	{
		app = (BaseApplication) ((Activity) context).getApplication();
		// 定位初始化
		mLocClient = new LocationClient(context.getApplicationContext());
		mLocClient.setAK(app.strKey);
		mLocClient.registerLocationListener(myloaction);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setAddrType("all");// all时，表示返回地址信息。
		option.disableCache(true);// 禁用缓存定位
		option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息
		mLocClient.setLocOption(option);
		mLocClient.start();
		search = new MKSearch();
		search.init(app.mBMapManager, new MMKSearchListener());
	}

	/**
	 * 定位SDK监听函数
	 */
	public class MyLocation implements BDLocationListener
	{
		@Override
		public void onReceiveLocation(BDLocation location)
		{
			if (location == null)
			{
				LogUtil.showCustomToast(context, "无法获得查询信息");
				return;
			}

			LogUtil.showCustomToast(context, location.getAddrStr());
			double longitude = location.getLongitude();// 经度
			double Latitude = location.getLatitude();// 纬度
			mpoint = new GeoPoint((int) (Latitude * 1e6),
					(int) (longitude * 1e6));
			search.poiSearchNearBy(keyWord, mpoint, 3000);
			System.out.println("**********************");
		}

		public void onReceivePoi(BDLocation poiLocation)
		{
			if (poiLocation == null)
			{
				return;
			}
		}
	}

	public void searchMost()
	{
		search.goToPoiPage(app.index);
	}

	/**
	 * 各搜索功能监听器
	 * 
	 * @author Administrator
	 */
	public class MMKSearchListener implements MKSearchListener
	{

		@Override
		public void onGetAddrResult(MKAddrInfo res, int error)
		{
			// TODO Auto-generated method stub
		}

		@Override
		public void onGetBusDetailResult(MKBusLineResult arg0, int arg1)
		{
			// TODO Auto-generated method stub
		}

		@Override
		public void onGetDrivingRouteResult(MKDrivingRouteResult res, int error)
		{
			// TODO Auto-generated method stub
		}

		@Override
		public void onGetPoiDetailSearchResult(int arg0, int arg1)
		{
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetPoiResult(MKPoiResult res, int type, int error)
		{
			// TODO Auto-generated method stub
			// 错误号可参考MKEvent中的定义
			if (error != 0 || res == null)
			{
				if (app.index > 1)
				{
				} else
				{
					LogUtil.showCustomToast(context, "抱歉，未找到结果");
				}
				handler.sendEmptyMessage(-1);
				return;
			}
			ArrayList<MKPoiInfo> list = res.getAllPoi();
			for (int i = 0; i < list.size(); i++)
			{
				String address = list.get(i).address;
				String city = list.get(i).city;
				String name = list.get(i).name;
				String phoneNum = list.get(i).phoneNum;
				int ePoiType = list.get(i).ePoiType;
				GeoPoint p1LL = mpoint;// 我当前位置的经纬度
				GeoPoint p2LL = list.get(i).pt;// 景点所在位置的经纬度
				// 经纬度计算。算当前位置距离景点的距离，返回米/1000 =公里
				double distance = DistanceUtil.getDistance(p1LL, p2LL) / 1000;
				// 实例化任务对象，存储对象信息到集合。
				Location myLocation = new Location(mpoint.getLatitudeE6(),
						mpoint.getLongitudeE6());
				Location scenicLocation = new Location(p2LL.getLatitudeE6(),
						p2LL.getLongitudeE6());
				Bundle bundle1 = new Bundle();
				bundle1.putParcelable("mylocation", myLocation);
				Bundle bundle2 = new Bundle();
				bundle2.putParcelable("scenicLocation", scenicLocation);
				String price = null;
				String img = null;
				switch (i)
				{
				case 1:
					img = "http://a.hiphotos.baidu.com/image/w%3D2048/sign=4f196eb68326cffc692ab8b28d394b90/7af40ad162d9f2d3af703cc0a8ec8a136327ccb8.jpg";
					phoneNum = "13115482884";
					price = "人均210元";
					break;

				case 3:
					img = "http://g.hiphotos.baidu.com/image/w%3D2048/sign=901945f7ac6eddc426e7b3fb0de3b7fd/0b55b319ebc4b745dd44dc04cefc1e178b8215cb.jpg";
					phoneNum = "13115482884";
					price = "人均61元";
					break;

				case 5:
					img = "http://f.hiphotos.baidu.com/image/w%3D2048/sign=21bdece97acb0a4685228c395f5bf724/b17eca8065380cd7bc6dacfea044ad34598281b5.jpg";
					phoneNum = "13991288126";
					price = "人均98元";
					break;
				case 7:
					img = "http://b.hiphotos.baidu.com/image/w%3D2048/sign=4aaacaca8f1001e94e3c130f8c367aec/8cb1cb13495409233432e05d9058d109b3de49be.jpg";
					phoneNum = "13912155454";
					price = "人均120元";
					break;
				case 9:
					img = "http://d.hiphotos.baidu.com/image/w%3D2048/sign=d39478e99d3df8dca63d8891f929738b/9f510fb30f2442a77a688bbbd343ad4bd113024d.jpg";
					phoneNum = "13912155454";
					price = "人均150元";
					break;
				default:
					img = "http://c.hiphotos.baidu.com/image/w%3D2048/sign=401105346b63f6241c5d3e03b37cebf8/902397dda144ad34eb8c098ad0a20cf431ad8588.jpg";
					phoneNum = "131121321248";
					price = "人均50元";
					break;

				}
				ScenicSpots spots = new ScenicSpots(img, address, name,
						phoneNum, price, distance, bundle1, bundle2);
				app.ScenicarrayList.add(spots);
			}
			if (app.ScenicarrayList.size() > 0 && handler != null)
			{
				handler.sendEmptyMessage(1);
			}
			app.index++;
		}

		@Override
		public void onGetShareUrlResult(MKShareUrlResult arg0, int arg1,
				int arg2)
		{
			// TODO Auto-generated method stub
		}

		@Override
		public void onGetSuggestionResult(MKSuggestionResult res, int arg1)
		{
			// TODO Auto-generated method stub

		}

		@Override
		public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)
		{
			// TODO Auto-generated method stub
		}

		@Override
		public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)
		{
			// TODO Auto-generated method stub
		}
	}

}